//
//  RadioPlayViewController.m
//  Leisure
//
//  Created by 大展 on 16/4/20.
//  Copyright © 2016年 张展. All rights reserved.
//

#import "RadioPlayViewController.h"

@interface RadioPlayViewController ()<UITableViewDataSource, UITableViewDelegate,PlayManagerDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIImageView *playImage;
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UILabel *time;
@property (strong, nonatomic) IBOutlet UISlider *slider;
@property (strong, nonatomic) IBOutlet UIButton *playOrPause;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;


@property (nonatomic, assign) NSInteger currentIndex;   //标识当前播放的下标

@end

@implementation RadioPlayViewController




// 设置界面信息
- (void)setInfo:(RedioDetailModel *)model {

    // 设置背景
    [self.playImage sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    // 设置title
    self.name.text = model.title;
    // 设置网页
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.playInfo.webview_url]]];
}

// 播放音乐
- (void)playRadio {

    [[PlayManager sharedManager]prepareToPlayRadioWithUrl:[self.dataSource[self.indexPath] musicUrl]];
    [[PlayManager sharedManager]playRadio];
}

// 上一首
- (IBAction)lastRadio:(UIButton *)sender {

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        if (self.indexPath == 0) {

            self.indexPath= self.dataSource.count-1;
        } else {

            self.indexPath --;
        }
        [self playRadio];
        dispatch_async(dispatch_get_main_queue(), ^{

            [self setInfo:self.dataSource[self.indexPath]];
        });
    });


}
// 播放
- (IBAction)playOrPause:(UIButton *)sender {

    if ([sender.currentTitle isEqualToString:@"暂停"]) {

        [[PlayManager sharedManager] pauseRadio];
        [sender setTitle:@"播放" forState:(UIControlStateNormal)];
    } else if ([sender.currentTitle isEqualToString:@"播放"]) {

        [[PlayManager sharedManager] playRadio];
        [sender setTitle:@"暂停" forState:(UIControlStateNormal)];
    }

}

// 下一首
- (IBAction)nextRadio:(UIButton *)sender {

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        if (self.indexPath == self.dataSource.count-1) {

            self.indexPath= 0;
        } else {

            self.indexPath ++;
        }
        [self playRadio];
        dispatch_async(dispatch_get_main_queue(), ^{

            [self setInfo:self.dataSource[self.indexPath]];
        });
    });
}
// 自动播放
- (void)playManagerPlayRadioEnd {

    [self nextRadio:nil];
}

// 视图将要出现的时候
- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [self playRadio];
    [self setInfo:self.dataSource[self.indexPath]];
}

// 下载
- (IBAction)loadAction:(UIButton *)sender {


}

// 滑轮
- (IBAction)clickSliderAction:(UISlider *)sender {


}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"RadioPlayListCell" bundle:nil] forCellReuseIdentifier:@"radioPlayList_cell"];

    [[PlayManager sharedManager] prepareToPlayRadioWithUrl:[self.dataSource[self.indexPath] musicUrl]];
    [[PlayManager sharedManager]playRadio];
    
    [PlayManager sharedManager].delegate = self;
    
    
}

-(void)playManagerFechRestTime:(NSString *)restTime progress:(CGFloat)progress{
    
    self.time.text = restTime;
    self.slider.value = progress;
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RadioPlayListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radioPlayList_cell" forIndexPath:indexPath];
    [cell setCellWithModel:self.dataSource[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {


}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
